package com.migu.rbac.controller;

import com.migu.rbac.service.AuthService;
import com.migu.rbac.service.SysUserService;
import com.migu.rbac.vo.GlobalConst;
import com.migu.rbac.vo.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO :
 * @Author: zj
 * @Date: 9:53 2019/4/9
 * @Version 1.0
 **/
@RestController
@RequestMapping("/api/v1/auth")
public class AuthController {

    private static final Logger log = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    private AuthService authService;

    @Autowired
    private SysUserService userService;
    /**
     * 登录
     *
     * @param loginName 用户名称
     * @param password  用户密码
     * @return
     */
    @PostMapping("/login")
    public Response login(@RequestParam("loginName") String loginName,
                          @RequestParam("password") String password,
                          @RequestParam("verification") String verification
    ) {
        return authService.login(loginName, password, verification);
    }

    //发送短信
    @RequestMapping(value = "/send-message", method = RequestMethod.POST)
    public Response sendMessage(@RequestParam("loginName") String loginName,
                                @RequestParam("password") String password) {
        return authService.sendMessage(loginName, password);
    }

    @PostMapping("/update-pwd")
    public Response updateUserPwdByUserInfo(@RequestParam String loginName, @RequestParam String phone,
                                            @RequestParam String password,
                                            @RequestParam String verification) {
        Response result = null;
        try {
            return userService.updateUserPwdByUserInfo(loginName, phone, password, verification);
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

    /**
     * 根据手机号修改密码
     *
     * @param loginName
     * @param phone
     * @return
     */
    @PostMapping("/sendMessagePwd")
    public Response updatePassword(@RequestParam String loginName, @RequestParam String phone) {
        Response result = null;
        try {
            return userService.sendMessageForUpPwd(loginName, phone);
        } catch (Exception e) {
            result = Response.error(GlobalConst.Code.FAIL_500, e.getMessage());
        }
        return result;
    }

}
